package org.itboys.mobile.service.mongo.front;

import com.google.common.collect.Maps;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayCollection;
import org.itboys.mobile.service.mysql.front.SundayFrontProductService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyang on 2017/12/20.
 * 圣代_收藏_service
 */
@Service
public class SundayFrontCollectionService extends BaseMongoService<SundayCollection>{

    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayCollection> getEntityClass() {
        return SundayCollection.class;
    }

    @Autowired
    private SundayFrontProductService productService;
    @Autowired
    private ResourceHolder resourceHolder;



    /**
     * 新增收藏
     * @param memberId
     * @param type  收藏的类型  自定义。1，商品,2用户,3分销产品（推荐人的产品）
     * @param otherId  收藏的东西id
     */
    public void saveCollection(Long memberId,Integer type,Long otherId){
        // List<SundayCollection> saveList = new ArrayList<SundayCollection>();
        Map<String, Object> param = Maps.newHashMapWithExpectedSize(4);
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId",memberId);
        param.put("type",type);
        param.put("otherId",otherId);
        long count = super.count(param);
        //取消收藏
        if(LongUtil.isNotZreo(count)){
            super.delete(param);
        }else{
            //增加收藏
            super.save(new SundayCollection(memberId,type,otherId));
        }
    }


    /**
     * 批量删除收藏
     * @param memberId
     * @param type  收藏的类型  自定义。1，商品
     * @param otherIds  收藏的东西id集合
     */
    public void deleteCollection(Long memberId,Integer type,List<Long> otherIds){
        //集合为空，不执行
        if(ListUtil.isNull(otherIds)){
            return;
        }
        Map<String, Object> param = Maps.newHashMapWithExpectedSize(4);
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId",memberId);
        param.put("type",type);
        param.put("otherId in",otherIds);
        super.delete(param);
    }
    /**
     * 批量新增收藏
     * @param memberId
     * @param type  收藏的类型  自定义。1，商品,2用户,3分销产品（推荐人的产品）
     * @param otherIds  收藏的东西id
     */
    public void saveCollection(Long memberId,Integer type,List<Long> otherIds){
        // List<SundayCollection> saveList = new ArrayList<SundayCollection>();
        Map<String, Object> param = Maps.newHashMapWithExpectedSize(4);
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId",memberId);
        param.put("type",type);
        //param.put("otherId",otherId);
       // long count = super.count(param);
        if(ListUtil.isNotNull(otherIds)){
            List<SundayCollection> collections =new ArrayList<>();
            for(Long otherId:otherIds){
                collections.add(new SundayCollection(memberId,type,otherId));
            }
            super.batchSaveWithoutLogin(collections);
        }

    }

    /**
     * 查询收藏otherIds
     * @param memberId  用户id
     * @param type  收藏的类型  自定义。1-商品,2-用户
     * @return 收藏的otherIds集合
     */
    public List<Long> selectCollection(Long memberId, Integer type){

        Map<String ,Object> param= new HashMap<String,Object>();
        List<Long> otherIds=new ArrayList<Long>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId",memberId);
        param.put("type",type);
        List<SundayCollection> collections = super.list(param);
        //没有收藏则直接返回空集合
        if(ListUtil.isNull(collections)){
            //return otherIds;
            //保持应用此集合查询时有数据，
            otherIds.add(0l);
        }
        for(SundayCollection collection : collections){
            otherIds.add(collection.getOtherId());
        }
        return otherIds;
    }



    /**
     * 判断是否收藏
     * @param memberId
     * @param type
     * @param otherId
     */
    public Integer checkIsCollection(Long memberId,Integer type,Long otherId){
        Map<String ,Object> param= new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId",memberId);
        param.put("otherId",otherId);
        param.put("type",type);
        if(super.count(param)>0){
            return 1;
        }else{
            return 0;
        }
    }
}
